package com.taoym.voucher.xcx.message;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @coder taoym
 * @datetime 2021/6/4 15:34
 * @desc 邮件发送
 */
@Component
@Slf4j
public class MyMailSender {

    @Resource
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String sender;

    /**
     * @coder taoym
     * @datetime 2021/6/4 15:34
     * @desc 邮件发送
     */
    public void send(String subject, String text, String email) {

        SimpleMailMessage message = new SimpleMailMessage();
        // 主题
        message.setSubject(subject);
        // 邮件发送者
        message.setFrom(sender);
        // 邮件接收者
        message.setTo(email);
        // 发送日期
        message.setSentDate(new Date());
        // 正文
        message.setText(text);
        log.info("开始发送邮件");
        javaMailSender.send(message);
        log.info("发送邮件完毕");
    }
}
